Passed
Pull Request — master (#220)
by
unknown
02:04
created

DocxAdapter.toBuffer   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
rs 9.8
c 0
b 0
f 0
cc 1
1
import * as pathlib from 'path';
2
import { Inject, Injectable } from '@nestjs/common';
3
import { Packer } from 'docx';
4
import { IDocxService } from 'src/Application/IDocxService';
5
import {
6
  DocxOptions,
7
  DocxFunction,
8
  DOCX_OPTIONS_TOKEN
9
} from '../docx.interfaces';
10
11
@Injectable()
12
export class DocxAdapter implements IDocxService {
13
  constructor(
14
    @Inject(DOCX_OPTIONS_TOKEN)
15
    private readonly options: DocxOptions
16
  ) {}
17
18
  public async toBuffer(
19
    name: string,
20
    locals: Record<string, any> = {}
21
  ): Promise<Buffer> {
22
    // Dynamically load docx generator function from:
23
    // {root}/{name}/docx.ts
24
    const path = pathlib.join(this.options.root, name, 'docx');
25
    const mod: any = await import(path);
26
    const fn: DocxFunction = mod.fn;
27
28
    const doc = fn(locals);
29
    return Packer.toBuffer(doc);
30
  }
31
}
32